package com.edu.servlet;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.ChartingRecords;
import com.edu.entity.CommonResult;
import com.edu.entity.User;
import com.edu.service.ChartingRecordsService;
import com.edu.service.impl.ChartingRecordsServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/getChaertingUsers.do")
public class QueryChartingUserServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final ChartingRecordsService crs = new ChartingRecordsServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        
        CommonResult cr = new CommonResult();
        
        try {
            // 获取查询参数
            String sender = request.getParameter("sendUsername");
//            System.out.println(999);
            System.out.println(sender+"----");
            // 验证参数
            if (sender == null) {
                cr.setCode(400);
                cr.setMessage("缺少必要参数: sender 或 receiver");
                response.getWriter().write(objectMapper.writeValueAsString(cr));
                return;
            }
            Set<User> userSets = crs.selectMyChartingObjs(sender);
            
            
            // 构建响应
            cr.setCode(200);
            cr.setMessage("success");
            cr.setData(userSets);
            
        } catch (Exception e) {
            e.printStackTrace();
            cr.setCode(500);
            cr.setMessage("服务器错误: " + e.getMessage());
        }
        
        // 返回JSON响应
        response.getWriter().write(objectMapper.writeValueAsString(cr));
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
       doGet(request, response);
    }

}